package org.intertwine.rssbot;

import org.jibble.pircbot.IrcException;

import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;

/**
 * Created by IntelliJ IDEA.
 * User: Stian Lindhom
 * Date: 21.feb.2010
 * Time: 16:21:19
 * To change this template use File | Settings | File Templates.
 */
public class RssBot {

    private PingBot db;

    private Properties properties;

    private final static String CONFIGURATION_FILENAME = "startup.properties";


    public RssBot() throws IOException {
        this.properties = new java.util.Properties();
        this.properties.load(RssBot.class.getClassLoader().getResourceAsStream(CONFIGURATION_FILENAME));
    }

    public void startUp() throws IOException, IrcException {

        RssReaders r = new RssReaders(new NewRSSItemListener() {
            public void onNewRssItem(String title) {
                if (db != null) {
                    for (String s : db.getChannels()) {
                        db.sendMessage(s, title);
                    }
                }
            }

            public void onFaultyRssFeed(String message) {
                for (String s : db.getChannels()) {
                    db.sendNotice(s, message);
                }

            }
        });
        FeedManager fm = new BasicFeedManager(r);

        db = new PingBot(fm, properties.getProperty("irc.nick"), properties.getProperty("irc.login"), properties.getProperty("irc.channels"), properties.getProperty("irc.masteruser"));
        db.setVerbose(Boolean.parseBoolean(properties.getProperty("irc.debug")));
        db.connect(properties.getProperty("irc.server"));
    }
}
